package com.ocm.tourism.core.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ocm.tourism.core.domain.RoleType;
import com.ocm.tourism.core.exception.ServicePermissionRuntimeException;
import com.ocm.tourism.core.util.UserUtil;


@Service("dispatchService")
public class DispatchServiceImpl implements DispatchService{

   private TourismService tourismClientService;

   private TourismService tourismAdminService;

   @Autowired
   public DispatchServiceImpl(@Qualifier("tourismClientService")TourismService tourismClientService, 
         @Qualifier("tourismAdminService")TourismService tourismAdminService){
      this.tourismClientService = tourismClientService;
      this.tourismAdminService = tourismAdminService;
   }

   @Override
   public TourismService getTourismService() {
      if (UserUtil.hasRole(RoleType.ROLE_ADMIN))
         return this.tourismAdminService;

      if (UserUtil.hasRole(RoleType.ROLE_USER))
         return this.tourismClientService;

      throw new ServicePermissionRuntimeException("The RoleType is not supported to retreive the TourismService service");
   }
}
